package problems

import "fmt"

type ListNode struct {
	Val int
	Next *ListNode
}

// Q2AddTwoNumbers
/**
	输入：l1 = [2,4,3], l2 = [5,6,4]
	输出：[7,0,8]
	解释：342 + 465 = 807.
 */
func Q2AddTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode  {
	root := &ListNode{}
	cursor := root

	t := 0
	for l1 != nil || l2 != nil || t != 0 {
		if l1 != nil {
			t += l1.Val
			l1 = l1.Next
		}
		if l2 != nil {
			t += l2.Val
			l2 = l2.Next
		}

		sumNode := &ListNode{Val: t % 10}
		cursor.Next = sumNode
		cursor = sumNode

		t /= 10
	}

	return root.Next
}

func CreateNode(nums []int) *ListNode {
	root := &ListNode{}
	cursor := root
	for _,v := range nums {
		node := &ListNode{Val: v}
		cursor.Next = node
		cursor = node
	}

	return root.Next
}


func ShowNode(list *ListNode) {
	cursor := list
	for cursor != nil {
		fmt.Println(cursor.Val)
		cursor = cursor.Next
	}
}


